package com.hexgon.plugins.rabbitmq.queue;

import com.hexgon.plugins.rabbitmq.MultiRabbitAdmin;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author: XuZhibin
 * @date: 2023/11/12 15:29
 * @version: v1.0
 * @attention:
 */
//@Component
public class AdminCreateQueue {

    @Autowired
    private MultiRabbitAdmin multiRabbitAdmin;

    @PostConstruct
    public void initRabbitMqQueueAndExchangeWithBinding() {
        // 正常QUEUE
        DirectExchange vipExchange = new DirectExchange("vipExchange", true, false);
        multiRabbitAdmin.adminInstance("default").declareExchange(vipExchange);
        Queue vipQueue = QueueBuilder.durable("vipQueue")
                .deadLetterExchange("vipDlExchange")
                .deadLetterRoutingKey("vip.info.dl")
                .build();
        multiRabbitAdmin.adminInstance("default").declareQueue(vipQueue);
        multiRabbitAdmin.adminInstance("default").declareBinding(
                BindingBuilder
                        .bind(vipQueue)        //直接创建队列
                        .to(vipExchange)    //直接创建交换机 建立关联关系
                        .with("vip.info"));    //指定路由Key

        // 死信QUEUE
        DirectExchange vipDlExchange = new DirectExchange("vipDlExchange", true, false);
        multiRabbitAdmin.adminInstance("default").declareExchange(vipDlExchange);
        Queue vipDlQueue = new Queue("vipDlQueue", true);
        multiRabbitAdmin.adminInstance("default").declareQueue(vipDlQueue);
        multiRabbitAdmin.adminInstance("default").declareBinding(
                BindingBuilder
                        .bind(vipDlQueue)        //直接创建队列
                        .to(vipDlExchange)    //直接创建交换机 建立关联关系
                        .with("vip.info.dl"));    //指定路由Key
    }

}
